#include<string>
#include<vector>
#include<array>
#include<unordered_map>
#include<algorithm>
#include<limits>
#include <stack>
#include<iostream>
#define MOD 1000000007
class Solution {
public:
    
    int maxWidthRamp(std::vector<int>& nums) {
        std::stack<int> st;
        int n = nums.size();
        st.push(0);
        for (int i=1; i<n; i++)
        {
            if (nums[st.top()]> nums[i])
            {
                st.push(i);
            }
        }
        int ans = 0;
        for (int j=n-1;j>=0;j--)
        {
            while (!st.empty() && nums[st.top()] <= nums[j])
            {
                int index = st.top();
                st.pop();
                ans = std::max(ans, j-index );
            }
        }
        return ans;
    }
};